import { calculateQimenPlate, getShiChen, getGanZhi } from '@/utils/qimen'
import { setStorage, getStorage } from '@/utils/storage'

const STORAGE_KEY = 'qimen_history'

export default {
  namespaced: true,
  
  state: {
    currentDate: new Date(),
    matter: '',
    plate: null,
    shiChen: '',
    ganZhi: {
      year: '',
      month: '',
      day: '',
      hour: ''
    },
    kongWang: {
      year: '',
      month: '',
      day: '',
      hour: ''
    },
    jieQi: '',
    lastJieQi: '',
    juShou: '',
    juNumber: 0,
    zhiFu: '',
    zhiShi: '',
    anZhiShi: '',
    history: []
  },
  
  mutations: {
    SET_CURRENT_DATE(state, date) {
      state.currentDate = date
    },
    
    SET_MATTER(state, matter) {
      state.matter = matter
    },
    
    SET_PLATE(state, plate) {
      state.plate = plate
    },
    
    SET_SHI_CHEN(state, shiChen) {
      state.shiChen = shiChen
    },
    
    SET_GAN_ZHI(state, ganZhi) {
      state.ganZhi = ganZhi
    },
    
    SET_KONG_WANG(state, kongWang) {
      state.kongWang = kongWang
    },
    
    SET_JIE_QI(state, jieQi) {
      state.jieQi = jieQi
    },
    
    SET_LAST_JIE_QI(state, lastJieQi) {
      state.lastJieQi = lastJieQi
    },
    
    SET_JU_SHOU(state, juShou) {
      state.juShou = juShou
    },
    
    SET_JU_NUMBER(state, juNumber) {
      state.juNumber = juNumber
    },
    
    SET_ZHI_FU(state, zhiFu) {
      state.zhiFu = zhiFu
    },
    
    SET_ZHI_SHI(state, zhiShi) {
      state.zhiShi = zhiShi
    },
    
    SET_AN_ZHI_SHI(state, anZhiShi) {
      state.anZhiShi = anZhiShi
    },
    
    ADD_HISTORY(state, record) {
      state.history.unshift(record)
      setStorage(STORAGE_KEY, state.history)
    },
    
    SET_HISTORY(state, history) {
      state.history = history
    }
  },
  
  actions: {
    // 更新当前时间
    updateCurrentTime({ commit, dispatch }, date) {
      commit('SET_CURRENT_DATE', date)
      dispatch('calculatePlate')
    },
    
    // 更新事项
    updateMatter({ commit }, matter) {
      commit('SET_MATTER', matter)
    },
    
    // 计算奇门盘
    calculatePlate({ state, commit }) {
      const { currentDate } = state
      const hour = currentDate.getHours()
      
      // 获取时辰
      const shiChen = getShiChen(hour)
      commit('SET_SHI_CHEN', shiChen)
      
      // 获取干支
      const ganZhi = getGanZhi(currentDate)
      commit('SET_GAN_ZHI', ganZhi)
      
      // 计算空亡
      const kongWang = calculateKongWang(ganZhi)
      commit('SET_KONG_WANG', kongWang)
      
      // 计算奇门盘
      const plate = calculateQimenPlate(
        currentDate,
        state.lastJieQi,
        ganZhi.day,
        ganZhi.hour
      )
      
      // 更新状态
      commit('SET_PLATE', plate)
      commit('SET_JIE_QI', plate.jieQi)
      commit('SET_JU_SHOU', plate.juShou)
      commit('SET_JU_NUMBER', plate.juNumber)
      commit('SET_ZHI_FU', plate.zhiFu)
      commit('SET_ZHI_SHI', plate.zhiShi)
      commit('SET_AN_ZHI_SHI', plate.anZhiShi)
      
      // 添加到历史记录
      commit('ADD_HISTORY', {
        date: currentDate,
        matter: state.matter,
        shiChen,
        ganZhi,
        kongWang,
        plate
      })
    },
    
    // 加载历史记录
    loadHistory({ commit }) {
      const history = getStorage(STORAGE_KEY) || []
      commit('SET_HISTORY', history)
    }
  }
}

// 计算空亡
function calculateKongWang(ganZhi) {
  // 这里需要实现空亡计算逻辑
  return {
    year: '戌亥',
    month: '戌亥',
    day: '戌亥',
    hour: '戌亥'
  }
} 